﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;

using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RestSharp;
namespace ImageSystem
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
          
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }
        string url = "https://sm.ms/api/upload";
        private void button2_Click(object sender, EventArgs e)
        {
           // string url = "https://sm.ms/api/upload";
            string file = Application.StartupPath + "/Red.png";
            var req = new RestRequest(Method.POST);
            req.AddFile("smfile", file);
           
            var restClient = new RestClient { BaseUrl = new Uri(url) };
            restClient.ExecuteAsync<Root>(req, (response) =>
            {
                if (response.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    Log(response.Content);
                    Log(response.Data.data.url);
                    Clipboard.SetText(response.Data.data.url);
                }

            });


        }
        private delegate void UpdLog(string value);

        public void Log(string value)
        {
            if (LogBox.InvokeRequired)
            {
                UpdLog t = new UpdLog(Log);
                this.LogBox.BeginInvoke(t, new object[] { value });
            }
            else
            {

                if (LogBox.Lines.Length > 2000)
                    LogBox.Clear();
                this.LogBox.AppendText(value + "\r\n");
            }
        }

        public void SetClip(string msg)
        {
            this.Invoke(new Action(delegate () {
                Clipboard.SetText(msg);

            }));
        }
      

        private void Form1_DragEnter(object sender, DragEventArgs e)
        {
            if(e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Link;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }

        }
        
        private void Form1_DragDrop(object sender, DragEventArgs e)
        {
            Array array = (System.Array)e.Data.GetData(DataFormats.FileDrop);
            int count = array.Length;
            string md = "";
            for(int i = 0;i<count;i++)
            {
               md+= UpdLoad(array.GetValue(i).ToString());
            }

            button1.Visible = true;
            //Clipboard.Clear();

          //  SetClip(md);


        }

        public string UpdLoad(string file)
        {
            var req = new RestRequest(Method.POST);
            req.AddFile("smfile", file);

            var restClient = new RestClient { BaseUrl = new Uri(url) };
            string result = "";
            restClient.ExecuteAsync<Root>(req, (response) =>
            {
                if (response.StatusCode == System.Net.HttpStatusCode.OK)
                {
                   
                    result="![图片](" +response.Data.data.url+ ")";
                    Log(result);
                    //  Clipboard.SetText(response.Data.data.url);
                }
               
            
            });
            return result;
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            Clipboard.Clear();

            Clipboard.SetText(LogBox.Text);
            button1.Visible = false;
            LogBox.Text = "";
        }
    }
}
